package newsCommentService

import (
	"gin/common/lib/redis"
	"gin/dao/entity"
	"gin/dao/mapper/news"
	"gin/dao/mapper/news/newsComment"
	"gin/model/body"
)

// AddComment 添加评论
func AddComment(p body.NewsCommentParam) {
	commentKey := redis.GetRedisKey("COMMENT_LIST", p.NewsId)
	redis.Del(commentKey)
	newsComment.Add(p)
	news.UpdateNum(p.NewsId, 3, true)
}

// DelComment 删除评论
func DelComment(p body.DelNewsParam) {
	commentKey := redis.GetRedisKey("COMMENT_LIST", p.NewId)
	redis.Del(commentKey)
	newsComment.Del(p)
	news.UpdateNum(p.NewId, 3, false)
}

func List(uid int64, p body.NewsCommentListParam) []*entity.NewsCommentDetail {
	commentKey := redis.GetRedisKey("COMMENT_LIST", p.NewId)
	list := make([]*entity.NewsCommentDetail, 0)
	err := redis.GetObject(commentKey, &list)
	if err == nil {
		return list
	}
	res := newsComment.List(p)

	newInfo := news.Detail(p.NewId)
	if newInfo.Uid == uid {
		for i, _ := range res {
			res[i].IsDel = true
		}
	} else {
		for i, v := range res {
			res[i].IsDel = v.Uid == uid
		}
	}

	news.UpdateNum(p.NewId, 1, true)
	redis.Set(commentKey, res, 60*60*24)

	return res
}

// UnreadNum 未读消息条数
func UnreadNum(uid int64) int64 {
	return newsComment.UnreadNum(uid)
}

// UnreadList 未读消息列表
func UnreadList(uid int64, p body.PageTrait) ([]*entity.NewsCommentDetail, int64) {
	return newsComment.UnreadList(uid, p)
}

// Read 已读消息
func Read(uid int64) {
	newsComment.Read(uid)
}
